package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: MemberController
 * @Author: majiahuan
 * @Date: 2020/3/20 8:06
 * @Description: 会员管理
 */
@RestController
@RequestMapping("/member")
public class MemberCountController {

    @Reference
    private MemberService memberService;

    //获取会员性别占比统计数据
    @RequestMapping("/getMemberSexCount")
    public Result getMemberSexCount() {
        try {
            Map<String, Object> memberSexCount = memberService.getMemberSexCount();//会员性别数量集合
            BigDecimal bigDecimal1 = (BigDecimal) memberSexCount.get("男");
            BigDecimal bigDecimal2 = (BigDecimal) memberSexCount.get("女");
            Integer memberMaleCount = Integer.parseInt(bigDecimal1.toString());
            Integer memberFemaleCount = Integer.parseInt(bigDecimal2.toString());
            Map<String, Integer> map = new HashMap<>();
            map.put("memberMaleCount", memberMaleCount);
            map.put("memberFemaleCount", memberFemaleCount);
            return new Result(true, "查询会员性别数量成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员性别数量失败");
        }
    }

    //获取会员性别占比统计数据
    @RequestMapping("/getMemberAge")
    public Result getMemberAge() {
        /*[
                        {value: res.data.data.setMemberAgeCount[0], name: '0-20'},
                        {value: res.data.data.setMemberAgeCount[1], name: '21-25'},
                        {value: res.data.data.setMemberAgeCount[2], name: '25-30'}
                    ]*/
        try {
            //定义会员年龄段集合
            List<String> memberAgeStage = new ArrayList<>();
            memberAgeStage.add("0-20");
            memberAgeStage.add("21-25");
            memberAgeStage.add("25-30");
            //根据年龄阶段获取对应的会员数量
            List<Map<String, Object>> setMemberAgeCount = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            for (String memberAge : memberAgeStage) {
                Map<String, Object> memberAgeCount = memberService.getMemberAge();//会员年龄数量集合
                BigDecimal bigDecimal = (BigDecimal) memberAgeCount.get(memberAge);
                Map<String, Object> temp = new HashMap<>();
                //Integer.parseInt(bigDecimal.toString())
                temp.put("value", Integer.parseInt(bigDecimal.toString()));
                temp.put("name", memberAge);
                setMemberAgeCount.add(temp);
            }
            map.put("setMemberAgeStage", memberAgeStage);
            map.put("setMemberAgeCount", setMemberAgeCount);
            return new Result(true, "查询会员年龄数量成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询会员年龄数量失败");
        }
    }
}
